package com.chepaandvasia.dao;

import com.chepaandvasia.entities.Address;
import com.chepaandvasia.entities.Car;
import com.chepaandvasia.entities.Customer;
import java.util.Collections;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;

@Repository
public class CustomerDAOImpl implements CustomerDAO {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public void create(Customer customer) {
		if (null == customer) {
			throw new IllegalArgumentException("Customer can not be null");
		}
		if (null != customer.getId()) {
			throw new IllegalArgumentException(
					"Customer already has an ID. It was stored already in database");
		}

		if (customer.getCustomerName() == null || customer.getAddress() == null) {
			throw new IllegalArgumentException(
					"Customer should fill his credentials and address");
		}
		// if everything is OK just save this customer in database
		entityManager.persist(customer);
	}

	@Override
	public Customer get(Long id) {
		if (id == null || id < 0) {
			throw new IllegalArgumentException(
					"ID can not be null and must be positive");
		}
		// if everything is OK just get customer in database
		Customer customer = entityManager.find(Customer.class, id);
		return customer;
	}

	@Override
	public void update(Customer customer) {
		if (customer == null) {
			throw new IllegalArgumentException("Customer can not be null");
		}

		if (customer.getId() == null || customer.getId() < 0) {
			throw new IllegalArgumentException(
					"Customer does not have an ID. It has not been stored in database yet");
		}
		// if everything is OK just update customer data in database
		entityManager.merge(customer);
	}

	@Override
	public void remove(Customer customer) {
		if (customer == null) {
			throw new IllegalArgumentException("Customer can not be null");
		}
		if (customer.getId() == null || customer.getId() < 0) {
			throw new IllegalArgumentException(
					"Customer does not have an ID. It has not been stored in database yet");
		}
		// if everything is OK just remove customer from database
		Customer v = entityManager.find(Customer.class, customer.getId());
		entityManager.remove(v);
	}

	@Override
	public List<Customer> findAllCustomersForCar(Car car) {
		TypedQuery<Customer> query = entityManager.createQuery(
				"SELECT c FROM Customer WHERE c.order = :car", Customer.class);
		query.setParameter("car", car);
		List<Customer> customerList = query.getResultList();
		return Collections.unmodifiableList(customerList);
	}

	@Override
	public List<Customer> findAllCustomers() {
		TypedQuery<Customer> query = entityManager.createQuery(
				"SELECT c FROM Customer c", Customer.class);
		// Query query=em.createQuery("SELECT v FROM Customer v");
		List<Customer> customerList = query.getResultList();
		return Collections.unmodifiableList(customerList);
	}

	@Override
	public List<Customer> getCustomersByAddress(Address address) {
		TypedQuery<Customer> query = entityManager.createQuery(
				"SELECT v FROM Customer v WHERE v.address = :address",
				Customer.class);
		// Query
		// query=em.createQuery("SELECT v FROM Customer v WHERE c.status=:status");
		query.setParameter("address", address);
		List<Customer> customerList = query.getResultList();
		return Collections.unmodifiableList(customerList);
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
}
